// movieDetailSubPackage/pages/movieDetail/movieDetail.js
import { getViewInfo } from '../../../utils/util'
import { recommandArr } from '../../../utils/dataSource'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        movieTitle: '',
        navData: [
            { text: '简介' },
            { text: '评论' }
        ],
        // 视频播放区域高度
        videoHeight: 0,
        currentTab: 0,
        navScrollLeft: 0,
        windowWidth: app.globalData.windowWidth,
        slideOffset: 0,
        navWidth: 0,
        recommandArr: recommandArr
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const { movieId, movieTitle } = options
        this.setData({ movieTitle })
        wx.setNavigationBarTitle({
            title: '十里电影-' + movieTitle,
        })
        // 计算视频区域的高度
        getViewInfo('#video').then(res => {
            this.setData({ videoHeight: res.height * 2 })
        })
        // 计算第一个tab的width
        getViewInfo('#nav0').then(res => {
            this.setData({ navWidth: res.width * 2, slideOffset: res.left })
        })
    },

    switchNav(event) {
        const cur = event.currentTarget.dataset.current;
        //每个tab选项宽度占1/5
        const singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.calculateCurTabWidth(cur)
            this.setData({
                currentTab: cur
            })
        }
    },
    switchTab(event) {
        const cur = event.detail.current;
        const singleNavWidth = this.data.windowWidth / 5;
        this.calculateCurTabWidth(cur)
        this.setData({
            currentTab: cur,
            navScrollLeft: (cur - 2) * singleNavWidth,
        });
    },

    // 计算当前tab的width
    calculateCurTabWidth(index){
        getViewInfo('#nav' + index).then(res => {
            this.setData({ navWidth: res.width * 2, slideOffset: res.left })
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})